/*********************************************************
 * File:OperAction.java
 * 
 * Version 1.0
 * 
 * Date     2003-12-26
 * Author   HuangYong
 * 
 * Copyright (C) 2003 HuangYong
 * all rights reserved.
 * 
 ********************************************************/
package com.vandagroup.portal.org;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.vandagroup.common.bo.UserContext;
import com.vandagroup.common.util.InputSet;
import com.vandagroup.common.webapp.ActiveAction;
import com.vandagroup.common.webapp.ResourceReader;
import com.vandagroup.portal.menu.treeview.CheckboxTreeView;

/**
 * @author HuangYong
 * 說明：各項操作流向控制
 */
public class OrgAction extends ActiveAction {

    /**
     * 根據不同的任務參數調用不同的函數進行業務處理
     */
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response,
        UserContext user)
        throws Exception {

        if (task == null || task.equals("")) {
            return forwardErrorPage(request, mapping, "操作類型參數丟失！");
        }

        if (step == null || step.equals("")) {
            return forwardErrorPage(request, mapping, "操作步驟參數丟失！");
        }

        if (task.equals("list")) {
            return doViewList(mapping, form, request, response, user);
        } else if (task.equals("add")) {
            return doAdd(mapping, form, request, response, user);
        } else if (task.equals("edit")) {
            return doEdit(mapping, form, request, response, user);
        } else if (task.equals("view")) {
            return doView(mapping, form, request, response, user);
        } else if (task.equals("delete")) {
            return doDelete(mapping, form, request, response, user);
        } else if (task.equals("selectOrg")) {
        	return doSelectOrg(mapping, form, request, response, user);
        } else {
            return forwardErrorPage(request, mapping, "操作類型參數不存在！");
        }

    }

    /**
     * 查看機構資訊列表
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @param user
     * @return ActionForward
     * @throws Exception
     * @throws SQLException
     */
    public ActionForward doViewList(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response,
        UserContext user)
        throws Exception, SQLException {
        try {
            if (step.equals("list")) {
                return forward(mapping.findForward("list"));
            } else {
                return forwardErrorPage(request, mapping, "操作步驟參數不存在！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return forwardErrorPage(request, mapping, "取機構列表資訊出錯！");
        }
    }

    /**
     * 添加機構資訊
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @param user
     * @return ActionForward
     * @throws Exception
     * @throws SQLException
     */
    public ActionForward doAdd(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response,
        UserContext user)
        throws Exception, SQLException {
       
            OrgFormBean bean = (OrgFormBean) form;
            //TODO modified by wdp
            CheckboxTreeView treeView =
                new CheckboxTreeView(
                    "功能列表", "FUNC", "FUNCID", "FUNCNAME",
                    "PARENTFUNCID", "$$$$$$$$", "funcID",
                    "FUNCID IN ( SELECT FUNCID FROM ORGFUNC WHERE ORGID = '"
                        + (bean.getUpOrgID().trim().equals("$$$$$$$$") ? bean.getOrgID() : bean.getUpOrgID())
                        + "' )");
                        
            
            /*CheckboxTreeView treeView =
            	new CheckboxTreeView(
            			"功能列表",
            			"FUNC",
            			"FUNCID",
            			"FUNCNAME",
            			"PARENTFUNCID",
            			"$$$$$$$$",
            			"funcID",
            			"FUNCID IN ( SELECT FUNCID FROM FUNC )");*/
            String contexPath=request.getContextPath().equals("/")?"":request.getContextPath();
            
            treeView.setContexPath(contexPath);

            treeView.create();
            request.setAttribute("treeView", treeView);
           
           /*debug
            System.out.println("FUNCID IN ( SELECT FUNCID FROM ORGFUNC WHERE ORGID = '"
                        + (bean.getUpOrgID().trim().equals("$$$$$$$$") ? bean.getOrgID() : bean.getUpOrgID())
                        + "' )");
            System.out.println("orgid:"+bean.getOrgID());
			System.out.println("uporgid:"+bean.getUpOrgID());
			*/

            if (step.equals("init")) {
            	OrgBO bo=new OrgBO();
            	InputSet set =new InputSet(bo.getRoles(user));
    			set.setType("checkbox");
    			request.setAttribute("roleID_SET",set);
                request.setAttribute("upOrgID", bean.getUpOrgID());
                return forward(mapping.findForward("add"));

            } else if (step.equals("submit")) {
                String funcID = request.getParameter( "funcID" );
                if( funcID == null ){
                	throw new Exception("請選擇一個功能號");
                }
                OrgBO bo = new OrgBO();
                bean.setOrgID(request.getParameter("OrgID"));
                int rtn = bo.add(bean);
                request.setAttribute("upOrgID", bean.getUpOrgID());
                request.setAttribute("success", "1");
                InputSet set =new InputSet(bo.getRoles(user));
    			set.setType("checkbox");
    			request.setAttribute("roleID_SET",set);
                return forward(mapping.findForward("add"));

            } else {
                return forwardErrorPage(request, mapping, "操作步驟參數不存在！");
            }

        
    }

    /**
     * 修改機構資訊資訊
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @param user
     * @return ActionForward
     * @throws Exception
     * @throws SQLException
     */
    public ActionForward doEdit(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response,
        UserContext user)
        throws Exception, SQLException {
        try {
            OrgBO bo = new OrgBO();
            OrgFormBean bean = (OrgFormBean) form;

            if (step.equals("init")) {
                bean = bo.retrieve(bean);

                CheckboxTreeView treeView =
                    new CheckboxTreeView(
                        "功能列表",
                        "FUNC",
                        "FUNCID",
                        "FUNCNAME",
                        "PARENTFUNCID",
                        "$$$$$$$$",
                        "funcID",
                        "FUNCID IN ( SELECT FUNCID FROM ORGFUNC WHERE ORGID = '"
                            + (bean.getUpOrgID().trim().equals("$$$$$$$$") ? bean.getOrgID() : bean.getUpOrgID())
                            + "' )",
                        bo.retrieveFuncs(bean.getOrgID()));
                if (bean.getOrgID().equals(user.getOrgID()))
                    treeView.setDisabled(true);
                String contexPath=request.getContextPath().equals("/")?"":request.getContextPath();
                
                treeView.setContexPath(contexPath);
                treeView.create();
                request.setAttribute("treeView", treeView);

                request.setAttribute("form", bean);
                return forward(mapping.findForward("edit"));

            } else if (step.equals("submit")) {

                CheckboxTreeView treeView =
                    new CheckboxTreeView(
                        "功能列表",
                        "FUNC",
                        "FUNCID",
                        "FUNCNAME",
                        "PARENTFUNCID",
                        "$$$$$$$$",
                        "funcID",
                        "FUNCID IN ( SELECT FUNCID FROM ORGFUNC WHERE ORGID = '"
                            + (bean.getUpOrgID().trim().equals("$$$$$$$$") ? bean.getOrgID() : bean.getUpOrgID())
                            + "' )",
                        bo.retrieveFuncs(bean.getOrgID()));
                treeView.create();
                request.setAttribute("treeView", treeView);

                String funcID = request.getParameter( "funcID" );
                if( funcID == null ){
                	throw new Exception("請選擇一個功能號");
                }
                
                int rtn = bo.update(bean, user);
                request.setAttribute("success", "1");
                return forward("/Org.do?task=edit&step=init&orgID=" + bean.getOrgID());

            } else {
                return forwardErrorPage(request, mapping, "操作步驟參數不存在！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return forwardErrorPage(request, mapping, "修改機構資訊出錯！");
        }
    }

    /**
     * 查看機構資訊資訊
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @param user
     * @return ActionForward
     * @throws Exception
     * @throws SQLException
     */
    public ActionForward doView(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response,
        UserContext user)
        throws Exception, SQLException {
        try {
            OrgFormBean bean = (OrgFormBean) form;
            OrgBO bo = new OrgBO();

            request.setAttribute("form", bo.retrieve(bean));
            return forward(mapping.findForward("view"));
        } catch (Exception e) {
            e.printStackTrace();
            return forwardErrorPage(request, mapping, "查看機構資訊出錯！");
        }
    }

    /**
     * 刪除機構資訊
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @param user
     * @return ActionForward
     * @throws Exception
     * @throws SQLException
     */
    public ActionForward doDelete(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response,
        UserContext user)
        throws Exception, SQLException {
        try {
            OrgFormBean bean = (OrgFormBean) form;
            OrgBO bo = new OrgBO();

            int rtn = bo.remove(bean);
            request.setAttribute("success", "deleteSuccess");
            return forward("/Org.do?task=add&step=init&upOrgID=" + bean.getRootOrgID());
        } catch (Exception e) {
            e.printStackTrace();
            return forwardErrorPage(request, mapping, "刪除機構資訊出錯！");
        }
    }
    
    /**
     * 選擇機構彈出視窗.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @param user
     * @return
     * @throws Exception
     */
	public ActionForward doSelectOrg(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response,
			UserContext user)
			throws Exception {
			if (step.equals("list")) {
				
				String strPageNO = (String) request.getParameter("pageNO");
				int pageNO = strPageNO == null 	? Integer.parseInt("1") : Integer.parseInt(strPageNO);
				try {
					OrgBO bo = new OrgBO();
					OrgFormBean bean = (OrgFormBean) form;
					
					com.vandagroup.common.util.Result t = bo.retrieveValidOrg( bean, pageNO );
					
					request.setAttribute("result", t);
	                request.setAttribute( "form", bean );
					return (mapping.findForward("selectOrg"));
				} catch (Exception e) {
					log.error("Exception:", e);
					return (forwardErrorPage(request, mapping, ResourceReader.getMessageFromResource("error.portal.org.search"), e));
				}
			} else {
				return (forwardErrorPage(request, mapping, ResourceReader.getMessageFromResource("error.steplose")));
			}
		}

}
